<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/10/26
 * Time: 17:14
 */

namespace app\api\controller;
require_once BASE_ROOT . "core/application/api/controller/BaseController.php";

use think\Request;
use think\Log;
use app\api\service\UserService;
use app\api\service\AddressService;
use app\api\service\GoodsService;

class User extends BaseController
{
    /**
     * 获取微信session_key
     * @code  登录小程序时 code
     * @desc 微信openid
     */
    public function OpenId()
    {
        $rs = array('code' => 0, 'info' => array());
        $code = Request::instance()->param('wx_code');
        $app_id = Request::instance()->param('i');       //小程序id
        $mch_id = $this->getMchId($app_id);
        $encryptedData = Request::instance()->param('encryptedData', null);
        $iv = Request::instance()->param('iv', null);
        $rule = [
            ['code', 'require'],
            ['mch_id', 'require', '不存在商户'],
        ];
        $data = [
            'code' => $code,
            'mch_id' => $mch_id,
        ];
        $result = $this->checkParam($rule, $data);
        if (!empty($result)) {
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        //存商户id
        $GLOBALS['mch_id'] = $mch_id;
        $user = new UserService();
        $info = $user->checkLogin($code, $encryptedData, $iv);
        $rs['info'] = $info;
        return json_encode($rs);
    }

    /**
     * 获取用户id
     * @return string
     */
    public function Login()
    {
        $rs = array('code' => 0, 'info' => array());
        $app_id = Request::instance()->param('i');       //小程序id
        $mch_id = $this->getMchId($app_id);
        $user_headimg = Request::instance()->param('user_headimg');
        $nick_name = Request::instance()->param('nick_name');
        $appid = Request::instance()->param('wx_openid');
        $wx_unionid = Request::instance()->param('wx_unionid');
        $pid = Request::instance()->param('pid', 0);
        $rule = [
            // ['user_headimg', 'require'],
            ['nick_name', 'require'],
            ['wx_openid', 'require'],
            ['mch_id', 'require', '不存在商户'],
        ];
        $data = [
            'user_headimg' => $user_headimg,
            'nick_name' => $nick_name,
            'wx_openid' => $appid,
            'mch_id' => $mch_id,
            'wx_unionid' => $wx_unionid,
            'reg_time' => time(),
            'is_distributor' => 0,
            'pid' => $pid
        ];
        $result = $this->checkParam($rule, $data);
        if (!empty($result)) {
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $user = new UserService();
        $info = $user->checkUser($appid, $pid, $mch_id);
        if (!empty($info)) {
            $rs['info'] = $info;
            return json_encode($rs);
        }
        $info = $user->addUser($data);
        if (empty($info)) {
            Log::write('用户添加失败 --' . $appid);
            $rs['code'] = 1;
            $rs['msg'] = "用户添加失败";
            return json_encode($rs);
        }
        $rs['info'] = $info;
        return json_encode($rs);
    }

    /**
     * 公众号登陆
     * @param $uid
     */
    public function WebLogin()
    {
        $rs = array('code' => 0, 'info' => array());
        $uid = Request::instance()->param('uid', 0);
        $userService = new UserService();
        $user = $userService->getUser($uid);
        if (empty($user)) {
            $rs['code'] = 1;
            $rs['msg'] = "用户登陆失败！";
            return json_encode($rs);
        }
        $rs['info'] = $user;
        return json_encode($rs);
    }

    /**
     * 解密
     * @return string
     */
    public function DecryptData()
    {
        $rs = array('code' => 0, 'info' => array());
        $app_id = Request::instance()->param('i');
        $encryptedData = Request::instance()->param('encryptedData');
        $sessionKey = Request::instance()->param('sessionKey');
        $iv = Request::instance()->param('iv');
        $rule = [
            ['encryptedData', 'require'],
            ['sessionKey', 'require'],
            ['iv', 'require'],
            ['app_id', 'require'],
        ];
        $data = [
            'encryptedData' => $encryptedData,
            'iv' => $iv,
            'sessionKey' => $sessionKey,
            'app_id' => $app_id,
        ];
        $result = $this->checkParam($rule, $data);
        if (!empty($result)) {
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $user = new UserService();
        $info = $user->decryptData($encryptedData, $sessionKey, $app_id, $iv, $data);
        if ($info == 0) {
            $rs['info'] = json_decode($data, true);
            return json_encode($rs);
        } else {
            $rs['code'] = 1;
            $rs['msg'] = $info;
            return json_encode($rs);
        }
    }

    /**
     * 用户中心数据
     * @return string
     */
    public function Index()
    {
        $rs = array('code' => 0, 'info' => array());
        $uid = Request::instance()->param('uid');       //用户id
        $app_id = Request::instance()->param('i');       //小程序id
        $mch_id = $this->getMchId($app_id);
        $rule = [
            ['uid', 'require'],
            ['mch_id', 'require', '不存在商户'],
        ];
        $data = [
            'uid' => $uid,
            'mch_id' => $mch_id,
            'app_id' => $app_id
        ];
        $result = $this->checkParam($rule, $data);
        if (!empty($result)) {
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $user = new UserService;
        $info = $user->orderCount($data);
        $rs['info'] = $info;
        return json_encode($rs);
    }

    public function UserInfo()
    {
        $rs = array('code' => 0, 'info' => array());
        $uid = Request::instance()->param('uid');       //用户id
        $app_id = Request::instance()->param('i');       //小程序id
        $mch_id = $this->getMchId($app_id);
        $rule = [
            ['uid', 'require'],
            ['mch_id', 'require', '不存在商户'],
        ];
        $data = [
            'uid' => $uid,
            'mch_id' => $mch_id,
        ];
        $result = $this->checkParam($rule, $data);
        if (!empty($result)) {
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $user = new UserService;
        $info = $user->get_userinfo($data);
        $rs['info'] = $info;
        return json_encode($rs);
    }

    /**
     * 关于我们
     * @return string
     */
    public function About()
    {
        $rs = array('code' => 0, 'info' => array());
        $app_id = Request::instance()->param('i');       //小程序id
        $mch_id = $this->getMchId($app_id);
        $rule = [
            ['mch_id', 'require', '不存在商户'],
            ['uid', 'require']
        ];
        $data = [
            'mch_id' => $mch_id,
            'uid' => Request::instance()->param('user_id', 0)
        ];
        $result = $this->checkParam($rule, $data);
        if (!empty($result)) {
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $index = new UserService;
        $info = $index->about($data);
        $rs['info'] = $info;
        return json_encode($rs);
    }

    /**
     * 地址添加
     * @return string
     */
    public function CreateAddress()
    {
        $rs = array('code' => 0, 'info' => array());
        $data = [
            'uid' => Request::instance()->param('uid'), //用户id
            'consigner' => Request::instance()->param('consigner'),     //收件人姓名
            'phone' => Request::instance()->param('phone'), //电话
            'area' => Request::instance()->param('areaid'), //地区id
            'address' => Request::instance()->param('address'), //详细地址
            'zip_code' => Request::instance()->param('zip_code'),   //邮编
            'is_default' => Request::instance()->param('is_default', 0),     //是否默认
        ];
        $rule = [
            ['uid', 'require|number'],
            ['consigner', 'require'],
            ['phone', 'require|number|length:11', '手机号格式不正确'],
            ['area', 'require|number'],
            ['address', 'require'],
        ];
        $result = $this->checkParam($rule, $data);
        if (!empty($result)) {
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $address = new AddressService();
        $info = $address->createAddress($data);
        if (empty($info)) {
            Log::write('用户地址添加失败 --' . $data['uid']);
            $rs['code'] = 1;
            $rs['msg'] = "用户地址添加失败";
            return json_encode($rs);
        }
        if ($info == "fail") {
            $rs['code'] = 1;
            $rs['msg'] = "您已成功添加地址";
            return json_encode($rs);
        }
        $rs['info'] = $info;
        return json_encode($rs);
    }

    /**
     * 用户地址
     * @return string
     */
    public function AddressList()
    {
        $rs = array('code' => 0, 'info' => array());
        $data = [
            'uid' => Request::instance()->param('uid'),     //用户id
        ];
        $rule = [
            ['uid', 'require|number'],
        ];
        $result = $this->checkParam($rule, $data);
        if (!empty($result)) {
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $page = Request::instance()->param('page', 1);
        $address = new AddressService();
        $info = $address->addressList($data, $page);
        if (empty($info)) {
            $rs['code'] = 1;
            $rs['msg'] = "用户无地址";
            return json_encode($rs);
        }
        $rs['info'] = $info;
        return json_encode($rs);
    }

    /**
     * 单条地址
     * @return string
     */
    public function SingleAddress()
    {
        $rs = array('code' => 0, 'info' => array());
        $data = [
            'id' => Request::instance()->param('id'),   //地址id
            'uid' => Request::instance()->param('uid'),//用户id
        ];
        $rule = [
            ['id', 'require|number'],
            ['uid', 'require|number'],
        ];
        $result = $this->checkParam($rule, $data);
        if (!empty($result)) {
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $address = new AddressService();
        $info = $address->singleAddress($data);
        if (empty($info)) {
            $rs['code'] = 1;
            $rs['msg'] = "用户无地址";
            return json_encode($rs);
        }
        $rs['info'] = $info;
        return json_encode($rs);
    }

    /**
     * 修改地址
     * @return string
     */
    public function UpdateAddress()
    {
        $rs = array('code' => 0, 'info' => array());
        $data = [
            'id' => Request::instance()->param('id'), //地址id
            'uid' => Request::instance()->param('uid'), //用户id
            'consigner' => Request::instance()->param('consigner'), //收件人
            'phone' => Request::instance()->param('phone'), //电话
            'area' => Request::instance()->param('areaid'), //地址id
            'address' => Request::instance()->param('address'),//详细地址
            'zip_code' => Request::instance()->param('zip_code'),   //邮编
            'is_default' => Request::instance()->param('is_default', 0), //是否默认
        ];
        $data = array_filter($data);
        $rule = [
            ['id', 'require|number'],
            ['uid', 'require|number'],
        ];
        $result = $this->checkParam($rule, $data);
        if (!empty($result)) {
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $address = new AddressService();
        $info = $address->updateAddress($data);
        if (empty($info)) {
            Log::write('用户地址添加失败 --' . $data['uid']);
            $rs['code'] = 1;
            $rs['msg'] = "用户地址添加失败";
            return json_encode($rs);
        }
        if ($info == "FAIL") {
            $rs['code'] = 1;
            $rs['msg'] = "用户不存在该地址";
            return json_encode($rs);
        }
        $rs['info'] = $info;
        return json_encode($rs);
    }

    /**
     * 删除地址
     * @return string
     */
    public function DelAddress()
    {
        $rs = array('code' => 0, 'info' => array());
        $data = [
            'id' => Request::instance()->param('id'),   //地址id
        ];
        $rule = [
            ['id', 'require|number'],
        ];
        $result = $this->checkParam($rule, $data);
        if (!empty($result)) {
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $address = new AddressService();
        $info = $address->delAddress($data);
        if (empty($info)) {
            Log::write('用户地址删除失败 --' . $data['id']);
            $rs['code'] = 1;
            $rs['msg'] = "用户地址删除失败";
            return json_encode($rs);
        }
        $rs['info'] = $info;
        return json_encode($rs);
    }

    /**
     * 用户收藏
     * @return string
     */
    public function GetFavorites()
    {
        $rs = array('code' => 0, 'info' => array());
        $data = [
            'uid' => Request::instance()->param('uid'),     //用户id
        ];
        $rule = [
            ['uid', 'require|number'],
        ];
        $page = Request::instance()->param('page', 1);
        $result = $this->checkParam($rule, $data);
        if (!empty($result)) {
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $goods = new GoodsService();
        $info = $goods->getFavorites($data, $page);
        if (empty($info)) {
            $rs['code'] = 1;
            $rs['msg'] = "无收藏商品";
            return json_encode($rs);
        }
        $rs['info'] = $info;
        return json_encode($rs);
    }

    public function GetAreaId()
    {
        $rs = array('code' => 0, 'info' => array());
        $areacode = Request::instance()->param('areacode');
        $data = [
            'areacode' => $areacode,     //用户id
        ];
        $rule = [
            ['areacode', 'require'],
        ];
        $result = $this->checkParam($rule, $data);
        if (!empty($result)) {
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $address = new AddressService();
        $info = $address->getAreaByCode($areacode);
        $rs['info'] = $info;
        return json_encode($rs);
    }
public function UpdIntegral(){
    $rs = array('code' => 0,'info' => array());
    $app_id = Request::instance()->param('i');       //小程序id
    $mch_id = $this->getMchId($app_id);
    $rule = [
        ['formid', 'require'],
        ['mch_id', 'require', '不存在商户'],
        ['uid', 'require'],
        ['out_trade_no', 'require'],
    ];
    $data = [
        'mch_id' => $mch_id,
        'formid' => Request::instance()->param('formid'),
        'uid' => Request::instance()->param('uid'),
        'out_trade_no' => Request::instance()->param('out_trade_no'),
    ];
    $result = $this->checkParam($rule, $data);
    if (!empty($result)) {
        $rs['code'] = 1;
        $rs['msg'] = $result;
        return json_encode($rs);
    }
    $order = Db::name('ybsc_order')->where(['mch_id' => $data['mch_id'],'buyer_id'=>$data['uid'], 'out_trade_no' => $data['out_trade_no']])->find();
    $jf=$order['jf_money'];
    if($jf>0){
        $user = Db::name('ybsc_user')->where('uid', $order['buyer_id'])->find();
        Db::name('ybsc_user')->where(['uid'=>$order['buyer_id']])->setDec('integral',$jf);
        Db::name('ybsc_integral_detail')->insert([
            'itype'=>2,
            'ctype'=>1,
            'integral_total'=>$user['integral'],
            'integral'=>$jf,
            'consume'=>0,
            'time'=>time(),
            'explain'=>'积分抵扣',
            'user_id'=>$order['buyer_id'],
            'mch_id'=>$order['mch_id'],
        ]);
    }
    return json_encode($rs);
}

}
